package com.example.bestudentsystem.demos.web.controller;

import com.example.bestudentsystem.demos.web.mapper.CourseMapper;
import com.example.bestudentsystem.demos.web.mapper.HistoryMapper;
import com.example.bestudentsystem.demos.web.mapper.SCMapper;
import com.example.bestudentsystem.demos.web.pojo.Course;
import com.example.bestudentsystem.demos.web.vo.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@CrossOrigin
public class courseController {
    @Autowired
    CourseMapper courseMapper;
    @Autowired
    SCMapper scMapper;
    @Autowired
    HistoryMapper historyMapper;
    @GetMapping("/getTimeTableByTno")
    public List<Course> getTimeTableByTno(@RequestParam("tno") String tno) {
        return courseMapper.getCourseByTno(tno);
    }
    @GetMapping("/getTimeTableBySno")
    public List<CourseVo> getTimeTableBySno(@RequestParam("sno") String sno) {
        System.out.println(sno);
        System.out.println(courseMapper.getCourseBySno(sno));
        return courseMapper.getCourseBySno(sno);
    }
    @GetMapping("/getSecourse")
    public List<CourseVo> getSecourse(@RequestParam("sno") String sno) {
        return courseMapper.getSecourse(sno);
    }
    @PutMapping("/slcourse")
    public void slCourse(@RequestParam("sno") String sno,@RequestParam("cno") String cno) {
        Timestamp htime = new Timestamp(System.currentTimeMillis());
        scMapper.addSC(sno,cno);
        historyMapper.addHistory(sno,cno,htime,"已选");
        courseMapper.UpdateResidue(cno,-1);
    }
   @GetMapping("/getMycourse")
   public List<CourseVo> getMycourse(@RequestParam("sno") String sno) {
       return courseMapper.getMycourse(sno);
   }
   @DeleteMapping("/delMycourse")
    public void delMycourse(@RequestParam("sno") String sno,@RequestParam("cno") String cno) {
        courseMapper.delMycourse(sno,cno);
       Timestamp htime = new Timestamp(System.currentTimeMillis());
       historyMapper.addHistory(sno,cno,htime,"已退");
       courseMapper.UpdateResidue(cno,1);
    }
    @GetMapping("/getStudentCourseTimes")
    public List<String> getStudentCourseTimes(@RequestParam("sno") String sno) {
        return courseMapper.getStudentCourseTimes(sno);
    }
}
